<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2015/12/16
 * Time: 15:21
 */

namespace BaseClass\Component\Article;
use Illuminate\Support\Facades\DB;

class ArticleLabel
{
    public $info;
    static function select($query_limit)
    {
        /*
         * $limit
         * |-user   按照用户筛选
         * |-sort   排序方式
         * |-num    每页条数
         * |-start  起始条数
         * |-desc   是否倒序列
         * |-article    关联文章查找
         * |-search     关键词查找
         * |-opposite  除开一篇文章 填入id
         *
         * $return_data
         * |-status 是否成功
         * |-message 消息
         * |-data   数据 DB返回的二维结构
         *
         */
        $query = DB::table("base_article_label");

        //排序
        if(  isset($query_limit["sort"])  )
        {
            //如果是降序排列
            if(isset($query_limit["desc"])  && true==$query_limit["desc"])
            {
                $query = $query->orderBy($query_limit["sort"],"desc");
            }
            else
            {
                $query = $query->orderBy($query_limit["sort"]);
            }

        }
        //如果没有指定排序方式，默认按照标签ID进行降序排列
        else
        {
            if(isset($query_limit["desc"])  && true==$query_limit["desc"])
            {
                $query = $query->orderBy("label_id","desc");
            }
            else
            {
                $query = $query->orderBy("label_id");
            }
        }


        //分页
        //起始条数
        if(isset($query_limit['start']))
        {
            $query = $query->skip($query_limit['start']);
        }

        //每页条数
        if(isset($query_limit["num"]))
        {
            if($query_limit["num"]==0)//如果指定0，直接就不用返回数据了
            {
                $return_data["status"] = true;
                $return_data["message"] = "查看到数据，但数量限制为0";
                $return_data["data"] = [];
                return $return_data;
            }
            $query = $query->take($query_limit["num"]);
        }
        else
        {
            $query = $query->take(config("my_config.default_num_page"));
        }



        if(isset($query_limit["article"]))
        {
            $query = $query->leftJoin("base_article_re_label","relation_label","=","label_id")
                ->leftJoin("base_article","relation_article","=","article_id")
                ->where("article_id","=",$query_limit["article"]);
        }
        elseif(isset($query_limit["opposite"]))
        {
            $query = $query->join("base_article_re_label","relation_label","=","label_id")
                ->join("base_article","relation_article","=","article_id")
                ->where("article_id","=",$query_limit["opposite"]);;

        }



        //关键词查找
        if(isset($query_limit["search"]))
        {
            $query = $query->where("label_name","like","%".$query_limit["search"]."%");
        }
        $data = $query ->get();
        //除开一篇文章

        if(isset($query_limit["opposite"]))
        {
            $allLabel = DB::table("base_article_label")->get();
            foreach($allLabel as $k1=>$v1)
            {
                foreach($data as $k2 =>$v2)
                {
                    if($v1->label_id == $v2->label_id)
                    {
                        unset($allLabel[$k1]);
                    }
                }
            }
            $data = $allLabel;
        }



        //按照条件查询完毕后，需要返回查询的数据
        $return_data["status"] = true;
        $return_data["message"] = "成功获取到数据";
        $return_data["data"] = $data;
        return $return_data;
    }
    /**
     * 构造函数
     * @param $label_id
     * @param $article_id
     */
    public function __construct($label_id)
    {
        $this->label_id = $label_id;
        $this->syncBaseInfo();
    }
    /**
     * 进行操作过后获取最新的文章标签
     * @access public
     * @return array
     */
    public function syncBaseInfo()
    {
        $this->info = DB::table('base_article_label')
            ->where('label_id','=',$this->label_id)
            ->first();

        return $this->info;
    }


    /**
     * 添加文章标签
     * @access public
     * @param $info_array
     */
    static function add($info_array)
    {
        //这里判断一下此条标签是否创建过
        if(DB::table("base_article_label")->where("label_name","=",$info_array['label_name'])->first())
        {
            return false;
        }
        $info_array['label_create_date'] = date("Y-m-d H:i:s");
        $r = DB::table("base_article_label");
        $r = $r->insert($info_array);


        return $r;
    }
    /**
     * 修改文章标签
     * @access public
     * @param $info_array
     */
    public function update($info_array)
    {
        $info_array['label_update_date'] = date("Y-m-d H:i:s");
        $r = DB::table("base_article_label")
            ->where("label_id","=",$this->label_id)->update($info_array);
        $this->syncBaseInfo();
        return $r;
    }
    /**
     * 删除文章标签
     * @access public
     */
    public function delete($user_id = NULL)
    {
        $r = DB::table("base_article_label")
            ->where("label_id","=",$this->label_id)->delete();
        return $r;
    }


    /**
     * 给文章添加标签
     * @access public
     * @param $article_id 被添加标签的文章ID
     */
    public function aAticleLabel($article_id)
    {
        //判断文章标签对是否已经有了
        if(DB::table("base_article_re_label")->where("relation_article","=",$article_id)
        ->where("relation_label","=",$this->label_id)->get() == null)
        {
            return DB::table("base_article_re_label")->insert(['relation_article'=>$article_id,'relation_label'=>$this->label_id]);
        }
        else
        {
            //标签已经添加过了
            return false;
        }
    }
    /**
     * 文章移除标签
     * @access public
     * @param $article_id 被移除标签的文章ID
     */
    public function removeLabel($article_id)
    {
        if(DB::table("base_article_re_label")->where("relation_article","=",$article_id)
                ->where("relation_label","=",$this->label_id)->delete())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}